package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.*;
import com.itheima.mapper.OrdersMapper;
import com.itheima.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>implements OrdersService {
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;

    @Override
    public void submit(Orders orders) {
        //获取当前的登录id
        Long userId = BaseContext.getCurrentId();
        //添加查询条件 userId
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //获取集合
        List<ShoppingCart> CartList = shoppingCartService.list(lqw);
        //根据userId 获取当前登录Id的user的内容
        User user = userService.getById(userId);
        //根据请求来的地址id获取
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        //设置一个orders的Id
        long ordersId = IdWorker.getId();
//     ------------------------------------------------------------------
        AtomicInteger amount = new AtomicInteger();
        List<OrderDetail> orderDetails =CartList.stream().map((item)->{
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setOrderId(ordersId);
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setNumber(item.getNumber());
            orderDetail.setAmount(item.getAmount());
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());

        orders.setId(ordersId);
        orders.setNumber(String.valueOf(ordersId));
        orders.setStatus(2);
        orders.setUserId(userId);
        orders.setAddressBookId(addressBookId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPayMethod(1);
        orders.setAmount(new BigDecimal(amount.get()));
        orders.setConsignee(addressBook.getConsignee());
        orders.setPhone(addressBook.getPhone());
        orders.setUserName(user.getName());
        orders.setAddress((addressBook.getProvinceName()==null?"":addressBook.getProvinceName())
                +(addressBook.getCityName()==null?"":addressBook.getCityName())
                +(addressBook.getDistrictName()==null?"":addressBook.getDistrictName())
                +(addressBook.getDetail()==null?"":addressBook.getDetail())
        );

        this.save(orders);

        orderDetailService.saveBatch(orderDetails);

        shoppingCartService.remove(lqw);




    }
}
//    @Autowired
//    private ShoppingCartService shoppingCartService;
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private AddressBookService addressBookService;
//
//    @Autowired
//    private OrderDetailService orderDetailService;
//
//    @Override
//    public void submit(Orders orders) {
//        //获取当前登录的Id
//        Long userId = BaseContext.getCurrentId();
//        //获取购物车的List集合
//        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
//        lqw.eq(ShoppingCart::getUserId,userId);
//        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
//        //判断购物车内是否有产品
//        if (shoppingCartList==null | shoppingCartList.size()==0){
//            throw new DandSException("购物车为空，不能下单");
//        }
//        //获取当前登录ID的user对象
//        User user = userService.getById(userId);
//
//        //获取当前登录ID的地址对象
//        Long addressBookId = orders.getAddressBookId();
//        AddressBook addressBook = addressBookService.getById(addressBookId);
//        //判断购物车内是否有产品
//        if (addressBook==null){
//            throw new DandSException("购物车为空，不能下单");
//        }
//
//        //获取一个id 用MP
//        long ordersId = IdWorker.getId();
//        AtomicInteger atomicInteger = new AtomicInteger();
//        List<OrderDetail> orderDetailList=shoppingCartList.stream().map((item)->{
//            OrderDetail orderDetail = new OrderDetail();
//            orderDetail.setName(item.getName());
//            orderDetail.setImage(item.getImage());
//            orderDetail.setOrderId(ordersId);
//            orderDetail.setDishId(item.getDishId());
//            orderDetail.setSetmealId(item.getSetmealId());
//            orderDetail.setDishFlavor(item.getDishFlavor());
//            orderDetail.setNumber(item.getNumber());
//            orderDetail.setAmount(item.getAmount());
//            atomicInteger.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
//            return orderDetail;
//        }).collect(Collectors.toList());
//        orders.setId(ordersId);
//        orders.setNumber(String.valueOf(ordersId));
//        orders.setStatus(2);
//        orders.setUserId(userId);
//        orders.setAddressBookId(addressBookId);
//        orders.setOrderTime(LocalDateTime.now());
//        orders.setCheckoutTime(LocalDateTime.now());
//        orders.setPayMethod(1);
//        orders.setAmount(new BigDecimal(atomicInteger.get()));
//        orders.setConsignee(addressBook.getConsignee());
//        orders.setPhone(addressBook.getPhone());
//        orders.setUserName(user.getName());
//        orders.setAddress((addressBook.getProvinceName()==null?"":addressBook.getProvinceName())
//                +(addressBook.getCityName()==null?"":addressBook.getCityName())
//                +(addressBook.getDistrictName()==null?"":addressBook.getDistrictName())
//                +(addressBook.getDetail()==null?"":addressBook.getDetail())
//        );
//        this.save(orders);
//
//        orderDetailService.saveBatch(orderDetailList);
//
//        shoppingCartService.remove(lqw);



